#include <iostream>
#include <string>
#include <cstring>
#include "TcpServer.hpp"
namespace nc_protocol
{
#define MYSELF 1
#define SPA " "
#define SPACE_LEN strlen(SPA)
#define INTERVAL "\r\n"
#define INT_LEN strlen(INTERVAL)
    class Request
    {
    public:
        // 1.自主实现
        // 2.使用现成的方案
        // _x _rule _y
        std::string serialization()
        {
#ifdef MYSELF
            std::string str;
            str = std::to_string(_x);
            str += SPA;
            str += _rule;
            str += SPA;
            str += std::to_string(_y);
            return str;
#else

#endif
        }
        // _x _rule _y
        bool Deserialization(const std::string &str)
        {
#ifdef MYSELF
            ssize_t left = str.find(SPA);
            if (left == std::string::npos)
                return false;
            ssize_t right = str.rfind(SPA);
            if (right == std::string::npos)
                return false;
            _x = atoi(str.substr(0, left).c_str());
            _y = atoi(str.substr(right + SPACE_LEN).c_str());
            _rule = str[left + SPACE_LEN];
#else

#endif
        }

    public:
        Request() {}
        Request(int x, int y, int rule) : _x(x), _y(y), _rule(rule)
        {
        }
        ~Request() {}

    public:
        int _x;
        int _y;
        char _rule;
    };
    class Response
    {
    public:
        // _code _result
        std::string serialization()
        {
#ifdef MYSELF
            std::string str;
            str = std::to_string(_code);
            str += SPA;
            str += std::to_string(_result);
            return str;
#else

#endif
        }
        // _code _result
        bool Deserialization(const std::string &str)
        {
#ifdef MYSELF
            ssize_t pos = str.find(SPA);
            if (pos == std::string::npos)
                return false;
            _code = atoi(str.substr(0, pos).c_str());
            _result = atoi(str.substr(pos + SPACE_LEN).c_str());
            return true;
#else

#endif
        }

    public:
        Response() {}
        Response(int result, int code)
            : _result(result), _code(code)
        {
        }
        ~Response() {}

    public:
        int _result;
        int _code; // 错误码
    };

    bool Recv(int sock, std::string *out)
    {
        // UDP是面向数据报:
        // TCP 面向字节流的:
        // recv ： 你怎么保证，你读到的inbuffer，是一个完整完善的请求呢？不能保证
        // "1234 + 5678" : 1234 +
        // "1234 + 5678" : 1234 + 5678 123+99
        // "1234 "
        // 必须是："1234 + 5678"
        // 单纯的recv是无法解决这个问题的，需要对协议进一步定制！
        char buffer[1024];
        ssize_t s = recv(sock, buffer, sizeof(buffer) - 1, 0);
        if (s > 0)
        {
            buffer[s] = 0;
            *out += buffer;
        }
        else if (s == 0)
        {
            return false;
        }
        else
        {
            return false;
        }
        return true;
    }

    void Send(int sock, const std::string str)
    {
        int n = send(sock, str.c_str(), str.size(), 0);
        if (n < 0)
            std::cout << "send error" << std::endl;
    }
    // length\r\n_x _rule _y\r\n
    std::string Decode(std::string &s)
    {
        std::size_t pos = s.find(INTERVAL);
        if (pos == std::string::npos)
            return "";
        int size = atoi(s.substr(0,pos).c_str());
        int surplus = s.size() - pos - 2 * INT_LEN;
        if (surplus >= size)
        {

            s.erase(0, pos + INT_LEN);
            std::string retString = s.substr(0, size);
            s.erase(0, size + INT_LEN);
            return retString;
        }
        else
            return "";
    }

    // length\r\n_x _rule _y\r\n
    std::string Encode(std::string &s)
    {
        std::string retString = std::to_string(s.size());
        retString += INTERVAL;
        retString += s;
        retString += INTERVAL;
        return retString;
    }

}